
import dgram from 'dgram'

import express from 'express'

import cors from 'cors'

let data = []

// 创建 http 服务器
const httpServer = express()

// 解决跨域问题
httpServer.use(cors())

httpServer.get('/getUdpData',(req,res)=>res.send(data))

httpServer.listen(3001,()=>console.log('httpServer run at 3001 port'))

 // 创建udp服务器
const udpServer = new dgram.Socket('udp4')

udpServer.on('message',msg=>{
    // data = [msg[73],msg[74],msg[75],msg[76]]
    console.log(msg);
    // console.log(data)
})

udpServer.on('error',error=>{
    console.log(error.stack)
    udpServer.close()
})

udpServer.on('listening',()=>{
  const address = udpServer.address();
  console.log(`服务器监听 ${address.address}:${address.port}`);
})

udpServer.bind(6301)

/*
import express from 'express'

import cors from 'cors'

import dgram from 'dgram'

let data = []

const httpServer = express()

httpServer.use(cors())

httpServer.get('/getUdpData',(req,res)=>{
    res.send(data)
})

httpServer.listen(3001,()=>console.log('appServer run at 3001' ))

const udpSocket = new dgram.Socket('udp4')

udpSocket.on('error',err=>{
    console.log(err.stack)
    serverSocket.close()
})

// 监听接收数据
udpSocket.on('message',msg=>{
    //data = [msg[73],msg[74],msg[75],msg[76]]
    //data = [msg[6],msg[7],msg[8],msg[9]]
    console.log(msg);
})

// 监听服务器开启
udpSocket.on('listening',()=>console.log('服务器已开启'))

udpSocket.bind(6302)
*/